package com.itljm.cxrMarket.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author ljm
 * @date 2024/11/4 17:23
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    //缓存基本对象，Integer String,实体类 等
    public <T> void setCacheObject(final String key,final T data){
        redisTemplate.opsForValue().set(key ,data ,10, TimeUnit.DAYS);
    }

    //缓存基本对象，Integer String,实体类 等
    // 自定义失效时间
    public <T> void setCacheObject(final String key,final T data,final  Integer timeout ,final TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key ,data ,timeout, timeUnit);
    }

    //设置失效时间
    public boolean expire(final String key ,final long timemout,final TimeUnit timeUnit){
        return redisTemplate.expire(key,timemout,timeUnit);
    }

    // 获取缓存的基本对象
    public <T>  T getCacheObject(final String key){
        ValueOperations<String ,T> operations =  redisTemplate.opsForValue();
        return operations.get(key);
    }

    //删除单个对象
    public boolean deleteObject(final String key){
        return redisTemplate.delete(key);
    }

    //删除集合对象
    public  long deleteObject(final Collection collection){
        return redisTemplate.delete(collection);
    }

    //缓存List 数据
    public <T> long setCacheList(final String key,final List<T> list){
        Long size =  redisTemplate.opsForList().rightPushAll(key,list);
        return size == null ? 0 : size;
    }

    // 获取List 缓存数据
    public <T> List<T> getCacheList(final String key){
        return  redisTemplate.opsForList().range(key ,0,-1);
    }

    //缓存Set数据
    public <T> BoundSetOperations<String ,T> setCacheSet(final String key, final Set<T> set){
        BoundSetOperations<String ,T> setOperations =  redisTemplate.boundSetOps(key);
        Iterator<T> it = set.iterator();
        while (it.hasNext()){
            setOperations.add(it.next());
        }
        return setOperations;
    }

    // 获取缓存的Set
    public  <T> Set<T> getCacheSet(final  String key){
        return redisTemplate.opsForSet().members(key);
    }

    // 缓存Map 数据
    public  <T> void setCacheMap(final String key,final Map<String ,T> map){
        if(map != null){
            redisTemplate.opsForHash().putAll(key,map);
        }
    }

    //获取缓存Map数据
    public <T> Map<String ,T> getCacheMap(final String key){
        return redisTemplate.opsForHash().entries(key);
    }

    //缓存Hash数据
    public  <T> void setCacheMapValue(final String key ,final String hkey,final T data){
        redisTemplate.opsForHash().put(key,hkey,data);
    }
    //获取缓存Hash数据
    public <T> T getCacheMapValue(final String key,final String hkey){
        HashOperations<String ,String ,T > operations = redisTemplate.opsForHash();
        return operations.get(key,hkey);
    }

    //删除缓存的Hash数据
    public void delCacheMapValue(final String key ,final String hkey){
        HashOperations operations = redisTemplate.opsForHash();
        operations.delete(key,hkey);
    }
}
